package org.boot.fast.pojo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 通用返回结果
 * @author 最爱芫荽
 * @email 15733440205@163.com
 */
@Data
public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    public final static Integer SUCCESS_CODE = 0;
    public final static Integer ERROR_CODE = 801;

    /** 错误码 **/
    @JsonProperty("code")
    private Integer code;

    /** 返回数据 **/
    @JsonProperty("data")
    private T data;

    /** 错误提示，用户可阅读 **/
    @JsonProperty("msg")
    private String msg;

    /** 当前时间 **/
    @JsonProperty("time")
    private LocalDateTime time;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = SUCCESS_CODE;
        result.msg = "";
        result.data = data;
        result.time = LocalDateTime.now();
        return result;
    }

    public static <T> CommonResult<T> success(T data, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.code = SUCCESS_CODE;
        result.msg = msg;
        result.data = data;
        result.time = LocalDateTime.now();
        return result;
    }

    public static <T> CommonResult<T> error(String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.code = ERROR_CODE;
        result.msg = msg;
        result.data = null;
        result.time = LocalDateTime.now();
        return result;
    }

    public static <T> CommonResult<T> error(int code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = msg;
        result.data = null;
        result.time = LocalDateTime.now();
        return result;
    }


}